<!--
 * @Author: wushuiqing
 * @Email: wsqsix@163.com
 * @Date: 2021-12-28 12:57:22
-->

<template>
  <div class="publish">
    <!-- 返回上一页 -->
    <el-button type="text" @click="goBack" size="18">返回</el-button>

    <!-- 表单开始 -->
    <el-form
      :model="infoForm"
      :rules="rules"
      ref="infoForm"
      label-width="100px"
      class="myForm"
    >
      <!-- 标题 -->
      <el-form-item label="标题" prop="title">
        <el-input v-model="infoForm.title"></el-input>
      </el-form-item>

      <!-- 所属栏目 -->
      <el-form-item label="所属栏目" prop="categoryId">
        <el-select v-model="infoForm.categoryId">
          <el-option
            v-for="item in cateData.data"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <!-- 封面 -->
      <el-form-item label="封面" prop="photo">
        <!-- 上传图片并显示缩略图 -->
        <el-upload
          v-model="infoForm.cover"
          class="avatar-uploader"
          :action="uploadFileURL"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <!-- 预览图片 -->
          <img
            v-if="imageUrl"
            :src="imageUrl"
            class="avatar"
            style="height: 100px"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        <div>图片大小不允许超过3M</div>
      </el-form-item>

      <!-- 正文 -->
      <el-form-item label="正文" prop="content">
        <tinymce v-model="infoForm.content" />
      </el-form-item>

      <!-- 发布 -->
      <el-form-item>
        <el-button type="primary" @click="onSubmit">发布</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import Tinymce from "@/components/Tinymce";
import { get, post } from "@/utils/request";
import { mapState } from "vuex";
export default {
  name: "demo",

  props: {},

  data() {
    return {
      infoForm: {},
      imageUrl: "",
      showFileURL: "http://121.199.29.84:8888/group1/",
      uploadFileURL: "http://121.199.29.84:8001/file/upload",
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
        content: [{ required: true, message: "请输入内容", trigger: "blur" }],
        categoryId: [
            { required: true, message: '请选择所属栏目', trigger: 'change' }
          ],
      },
      cateData:{}
    };
  },

  computed: {
    ...mapState("user", ["userId", "name"]),
  },

  components: {
    Tinymce,
  },

  watch: {},

  methods: {
    goBack() {
      this.$router.go(-1);
    },
    onSubmit() {
      // console.log(this.infoForm);
      this.$refs["infoForm"].validate((valid) => {
        if (valid) {
          this.infoForm.authorId = this.userId;
          this.infoForm.baseUser = {};
          this.infoForm.baseUser.realname = { ...this.name };
          post("/article/saveOrUpdate", this.infoForm).then((res) => {
            // console.log(res);
            if (res.message == "更新成功") {
              this.$message({
                message: res.message,
                type: "success",
              });
              this.$router.go(-1);
            } else {
              this.$message({
                type: "error",
              });
            }
          });
        } else {
          return false;
        }
      });
    },

    handleAvatarSuccess(res, file) {
      // console.log(res);
      this.imageUrl = URL.createObjectURL(file.raw);
      this.infoForm.cover = this.showFileURL + res.data.id;
    },

    beforeAvatarUpload(file) {
      const isLt3M = file.size / 1024 / 1024 < 3;
      if (!isLt3M) {
        this.$message.error("上传图片大小不能超过 3MB!");
      }
    },
  },

  created() {
    this.infoForm = this.$route.query;

    get("/category/findCategoryTree").then((res) => {
      this.cateData = res;
    });
  },

  mounted() {},

  beforeDestroy() {},

  destroyed() {},
};
</script>

<style scoped>
.myForm {
  padding: 40px;
}

.avatar-uploader-icon {
  border: 1px dashed #d9d9d9;
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
</style>
